//
//  HOQuestionDetailViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 08/05/2017.
//  Copyright © 2017 WeiDu. All rights reserved.
//

#import "HOQuestionDetailViewController.h"
#import "HOConstants.h"
#import "HOUtils.h"
#import "HOQuestionReplyViewController.h"

@interface HOQuestionDetailViewController ()

@end

@implementation HOQuestionDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableView];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	//	[self setNavigationItemTitle:self.questionEntity.questionTitle];
	[self setNavigationItemTitle:NSLocalizedString(@"问题详情", @"问题详情")];
	[self displayRightButtonItemWithTitle:NSLocalizedString(@"追加提问", @"追加提问")];
	[self requestDataWithQuestionId:self.questionEntity.questionId];
}

- (void)rightButtonItemDidClick
{
	HOQuestionReplyViewController *viewCtrl = [[HOQuestionReplyViewController alloc] init];
	viewCtrl.questionEntity = self.questionEntity;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}



- (void)requestDataWithQuestionId:(int)questionId
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetQuestionDetail,
								  kHO_FIELD_QuestionId:[NSNumber numberWithInt:questionId]}];
}



#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOReplyEntity *entity = [self.questionEntity.replyList objectAtIndex:indexPath.section];
	UILabel *locationLbl = [[[UILabel alloc]
							 initWithFrame:CGRectMake(40,
													  8,
													  baseTableview.frame.size.width - 40 - 50,
													  1000)]autorelease];
	locationLbl.numberOfLines = 3;
	locationLbl.text = [NSString stringWithFormat:@"%@：%@", NSLocalizedString(@"提问", @"提问"), entity.replyContent];
	[locationLbl sizeToFit];
	return 16 + 25 + locationLbl.frame.size.height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	NSInteger count = [self.questionEntity.replyList count];
	[self updateNoResultView:count];;
	return count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 1;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	if(section == 0)
		return 10;
	return [HOUtils defaultOnePixelConversion];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *HOTableViewCell = @"HOTableViewCell";
	UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOTableViewCell];
	if(cell == nil)
	{
		cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOTableViewCell] autorelease];
	}
	
	NSInteger count = [self numberOfSectionsInTableView:tableView];
	
	HOReplyEntity *entity = [self.questionEntity.replyList objectAtIndex:(count - indexPath.section - 1)];
	UILabel *typeLbl = (UILabel *)[cell viewWithTag:115];
	
	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
	if(typeLbl == nil)
	{
		typeLbl = [[UILabel alloc]
				   initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
							   8,
							   tableView.frame.size.width - 40,
							   22)];
		typeLbl.tag = 115;
		[cell.contentView addSubview:typeLbl];
		typeLbl.textColor = TEXT_COLOR_666666;
		typeLbl.font = [UIFont boldSystemFontOfSize:15];
		[typeLbl release];
	}
	typeLbl = (UILabel *)[cell viewWithTag:115];
	typeLbl.text = entity.isAskerReply ? NSLocalizedString(@"问:", @"问:") : NSLocalizedString(@"答:", @"答:");
	typeLbl.textColor = entity.isAskerReply ? [UIColor blueColor] : [UIColor redColor];
	
	UILabel *replyContentLbl = (UILabel *)[cell viewWithTag:116];
	if(replyContentLbl == nil)
	{
		replyContentLbl = [[UILabel alloc]
						   initWithFrame:CGRectMake(40,
													8,
													baseTableview.frame.size.width - 40 - 50,
													1000)];
		replyContentLbl.tag = 116;
//		replyContentLbl.textAlignment = NSTextAlignmentRight;
		replyContentLbl.numberOfLines = 0;
//		replyContentLbl.layer.borderWidth = 1;
		[cell.contentView addSubview:replyContentLbl];
		replyContentLbl.textColor = TEXT_COLOR_666666;
		replyContentLbl.font = [UIFont systemFontOfSize:15];
		[replyContentLbl release];
	}
	replyContentLbl = (UILabel *)[cell viewWithTag:116];
	replyContentLbl.text = entity.replyContent;
	[replyContentLbl sizeToFit];
//	replyContentLbl.text = [HOUtils getDateFromTimeInterval:entity.createTime * 1000];
	
	UILabel *timeLbl = (UILabel *)[cell viewWithTag:117];
	if(timeLbl == nil)
	{
		timeLbl = [[UILabel alloc]
				   initWithFrame:CGRectMake(self.view.frame.size.width - 200,
											timeLbl.frame.origin.y + 20,
											160,
											22)];
		timeLbl.tag = 117;
		timeLbl.numberOfLines = 3;
		[cell.contentView addSubview:timeLbl];
		timeLbl.textColor = TEXT_COLOR_999999;
		timeLbl.font = [UIFont systemFontOfSize:13];
		timeLbl.textAlignment = NSTextAlignmentRight;
		[timeLbl release];
	}
	timeLbl = (UILabel *)[cell viewWithTag:117];
	timeLbl.frame = CGRectMake(HO_HORIZONAL_PADDING,
							   replyContentLbl.frame.origin.y + replyContentLbl.frame.size.height + 8,
							   110,
							   22);
	timeLbl.text = [HOUtils getDateFromTimeInterval:entity.replyTime * 1000];
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
}
#pragma mark - -- tool
- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}


- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetQuestionDetail])
	{
		[self.questionEntity parseDetailDic:response.dataNode];
	}
}

@end
